var ws, aid, str, hid, did, dname, djob, offices, rmoney, imoney, uid, spage, sex='男';
var total = 0, info, balance;
document.addEventListener('plusready', function(){
	ws = plus.webview.currentWebview();
	balance = plus.storage.getItem('money');
	uid = plus.storage.getItem('uid');
	hid = ws.hid;	//医院id
	aid = ws.aid;	//选择的时间段id
	did = ws.did;	//医生id
	dname = ws.dname;	//医生名
	djob = ws.djob;	//医生级别
	offices = ws.offices;	//就诊科室
	str = ws.str;	//预约时间
	spage = ws.spage;
	console.log(hid+" "+str+" "+dname+" "+djob+" "+offices+" "+balance);
	var Array = [str, offices, dname, djob];
	$('.div-first p').each(function(e){
		$(this).find('span').eq(1).text(Array[e]);
	})
	//查询该医院的挂号费及信息服务费
	console.log(apiRoot+"/home/bespoke/infoMoney/hid/"+hid);
	$.ajax({
		type:"get",
		url:apiRoot+"/home/bespoke/infoMoney",
		data:{hid:hid},
		dataType :'json',
		success :function(data){
//			console.log(JSON.stringify(data));
			rmoney = parseFloat(data.rmoney).toFixed(2);
			imoney = parseFloat(data.rmoney).toFixed(2);
			total = (parseFloat(data.rmoney) + parseFloat(data.rmoney)).toFixed(2);
			$('.registered').text('¥' + rmoney);
			$('.info').text('¥' + imoney);
			$('.total').text('¥' + total);
		},
		error :function(e){
			console.log(JSON.stringify(e));
		}
	});
	$('input[type=radio]').on('tap', function(){
		sex = $(this).siblings('label').text();
	})
	//点击提交
	$('button').on('tap', function(){
		var isNull = false;
		$('input').each(function() {
			var _this = $(this);
			var _val = _this.val();
			if((_this.attr('name') == 'name' || _this.attr('name') == 'age' || _this.attr('name') == 'phone') && !_val.trim()) {
				toast('请填写' + _this.siblings('label').text());
				setTimeout(function () {
					_this.focus();
				},50);
				isNull = true;
				return false;
			}
			var base;
			switch(_this.attr('name')){
				case 'idcard':base = card;break;
				case 'age':base = a;break;
				case 'phone':base = p1;break;
//				case 'weight':base = w;break;
				case 'height':base = h;break;
			}
			if(_val && !_val.match(base)){
				console.log(_val+" "+base);
				toast(_this.siblings('label').text()+'格式不正确');
				setTimeout(function () {
					_this.focus();
				},50);
				isNull = true;
				return false;
			}
		});
		if(isNull) {
			return;
		}
		info = $('form[name=info]').serialize();
		info = info+"&timeslot="+$('.times').text()+
				"&offices="+$('.offices').text()+
				"&doctor="+did+"&level="+$('.level').text()+
				"&uid="+uid+"&rmoney="+rmoney+"&imoney="+imoney+
				"&total="+total+"&hospital="+hid+"&sid="+aid+"&sex="+sex;
//		console.log(JSON.stringify(data));
		$('.zhifzzao').show();
    	$('.zhiftc').slideDown();
		
	})
	//支付方式
	$('.zhiftc div').on('tap', function(){
		switch($(this).index()){
			case 1://点击微信支付
			ptype = 'wxpay'; 
			payment(total);
			break;
			case 2://点击支付宝支付
			ptype = 'alipay'; 
			payment(total);
			break;
			case 3://点击银联支付
			toast('待接口提供');return;
			break;
			case 4://点击余额支付
			console.log(total+ " "+balance);
			if(parseFloat(balance) > parseFloat(total)){
				ReBalance();
			}else{
				toast('您的余额不足请及时充值');
			}
			break;
		}
	})
},false)
//减少账户余额
function ReBalance(){
	$.ajax({
		type:"get",
		url:apiRoot+"/home/income/Decbalance",
		data :{
			uid :uid,
			money :total
		},
		success:function(data){
			console.log(data);
			var str = parseFloat(balance) - parseFloat(total);
			plus.storage.setItem('money', str+'');
			source();
		},
		error :function(e){
			console.log(JSON.stringify(e));
		}
	});
}
function source(){
	$.ajax({
		type:"post",
		url:apiRoot+"?m=home&c=bespoke&a=addBooking",
		data :info,
		success :function(data){
			if(data){
				toast('提交成功');
				if(spage)plus.webview.getWebviewById(spage).close();
				ws.close();
			}else{
				toast('提交失败');
			}
		},
		error :function(e){
			errortoast();
			console.log(JSON.stringify(e));
		}
	});
}
